package com.lanou.myim.tools;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.widget.ImageView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.easemob.chat.EMChatManager;
import com.lanou.myim.application.BaseApplication;

import org.jivesoftware.smack.sasl.SASLMechanism;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Risky on 15/10/21.
 */
public class VolleySingleton {

    private RequestQueue queue;
    private ImageLoader imageLoader;

    private VolleySingleton() {
        queue = getQueue();
        imageLoader = new ImageLoader(queue, new DoubleCache());
    }

    private static final class SingletonHolder {
        private static final VolleySingleton instance = new VolleySingleton();
    }

    public static VolleySingleton getInstance() {
        return SingletonHolder.instance;
    }

    private RequestQueue getQueue() {
        if (queue == null) {
            queue = Volley.newRequestQueue(BaseApplication.applicationContext);
        }
        return queue;
    }

    public static final String TAG = "VolleySingleton";

    public <T> void _addRequest(Request<T> request) {
        request.setTag(TAG);
        queue.add(request);
    }

    public <T> void _addRequest(Request<T> request, Object tag) {
        request.setTag(tag);
        queue.add(request);
    }

    public void _addRequest(String url, Response.Listener<String> listener,
                            Response.ErrorListener error) {
        StringRequest stringRequest = new StringRequest(url, listener, error);
        _addRequest(stringRequest);
    }

    public <T> void _addRequest(String url, Class<T> clazz, Response.Listener<T> success, Response.ErrorListener failed) {
        GsonRequest<T> request = new GsonRequest<T>(url, clazz, success, failed);
        _addRequest(request);
    }

    public void removeRequest(Object tag) {
        queue.cancelAll(tag);
    }

    public ImageLoader _getImageLoader() {
        return imageLoader;
    }

    public static void addRequest(String url, Response.Listener<String> listener,
                                  Response.ErrorListener error) {
        getInstance()._addRequest(url, listener, error);
    }

    public static <T> void addRequest(String url, Class<T> clazz, Response.Listener<T> success, Response.ErrorListener failed) {
        getInstance()._addRequest(url, clazz, success, failed);
    }

    public static ImageLoader getImageLoader(){
        return getInstance()._getImageLoader();
    }

    public void addImageRequest(String url, final String fileName, final Context context, final ImageView imageView){
        final String cacheDir = Environment.getExternalStorageDirectory().getAbsolutePath()
                +"/Android/data/com.lanou.myim/easemob-demo#chatdemoui/"
                + EMChatManager.getInstance().getCurrentUser()+"/image";
        final File imageFile = new File(cacheDir, fileName);
        if (!imageFile.exists()){
            ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                    FileOutputStream fileOutputStream = null;
                    try {
                        imageFile.createNewFile();
                        fileOutputStream = new FileOutputStream(imageFile);
                        response.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (fileOutputStream != null) {
                            CloseHelper.close(fileOutputStream);
                        }
                    }

                imageView.setImageBitmap(BitmapUtils.getLoacalBitmap(cacheDir+fileName,100,100));
            }
        }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        _addRequest(imageRequest);
        }else {
            imageView.setImageBitmap(BitmapUtils.getLoacalBitmap(cacheDir+fileName,100,100));
        }
    }
}
